<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8" />
    <title>评论管理_资源共享平台</title>
    <link rel="stylesheet" href="/layui/css/layui.css"/>
    <link rel="stylesheet" href="/css/style.css"/>
</head>
<body>
<div th:include="common/header" class="header-wrapper"></div>
<div class="container-wrapper">
    <div th:include="common/menu" class="menu-left"></div>
    <div class="menu-right" style="margin-left: 20px">
        <fieldset class="layui-elem-field layui-field-title">
            <legend>评论管理</legend>
        </fieldset>
        <table id="commentListTable" lay-filter="commentListTable"></table>
    </div>
</div>
<div th:include="common/link" class="wrapper"></div>
<div th:include="common/footer" class="footer-wrapper"></div>

<script type="text/html" id="toolbarTpl">
    <button class="layui-btn layui-btn-sm" lay-event="delete">删除</button>
</script>
<script type="text/html" id="toolTpl">
    <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除</a>
</script>
<script src="/layui/layui.js"></script>
<script src="/js/common.js"></script>
<script type="text/javascript" th:inline="javascript">
    /*<![CDATA[*/
    let $, layer, table;
    layui.use(['layer', 'element', 'table'], function () {
        $ = layui.jquery;
        layer = layui.layer;
        table = layui.table;

        table.render({
            elem: '#commentListTable',
            toolbar: '#toolbarTpl',
            url: '/comment/userCommentList',
            cols: [[
                {type: 'checkbox'},
                {field: 'content', width: 350, title: '评论内容'},
                {field: 'res', width: 100, title: '评论资源', templet: formatResName},
                {field: 'createDate', width: 60, title: '评论日期', sort: true, align: 'center'},
                {field: 'user', width: 120, title: '评论用户', templet: formatUsername},
                {field: 'action', width: 150, title: '操作', align: 'center', templet: '#toolTpl'}
            ]],
            page: true
        });

        // 触发工具栏事件
        table.on('toolbar(commentListTable)', function (obj) {
            switch (obj.event) { // 获得 lay-event 对应的值
                case 'delete': // 删除
                    deleteSelected();
                    break;
            }
        });

        // 触发单元格工具事件
        table.on('tool(commentListTable)', function (obj) {
            let data = obj.data; // 获得当前行数据
            switch (obj.event) { // 获得 lay-event 对应的值
                case 'delete': // 删除
                    deleteOne(data.id);
                    break;
            }
        });
    });

    function formatResName(d) {
        return "<a href='/res/detail/" + d.res.id + "' target='_blank'>" + d.res.name + "</a>";
    }

    function formatUsername(d) {
        return d.user.username;
    }

    function deleteOne(id) {
        layer.confirm('您确定要删除这条记录吗？', {
            title: "系统提示",
            btn: ['确定', '取消'] //按钮
        }, function () {
            layer.closeAll('dialog');
            $.post("/comment/delete", {"id": id}, function (result) {
                if (result.success) {
                    layer.msg("删除成功！");
                    table.reload("commentListTable", {});
                } else {
                    layer.msg("删除失败，请联系管理员！");
                }
            }, "json");
        }, function () {

        });
    }

    function deleteSelected() {
        let check = table.checkStatus('commentListTable');
        if (check.data.length == 0) {
            layer.msg("请选择要删除的数据！");
            return;
        }
        let strIds = [];
        let rows = check.data;
        for (let i = 0; i < check.data.length; i++) {
            strIds.push(rows[i].id);
        }
        let ids = strIds.join(",");
        layer.confirm('您确定要删除这<label style="color: red;">' + check.data.length + '</label>条记录吗？', {
            title: '系统提示',
            btn: ['确定', '取消'] //按钮
        }, function () {
            layer.closeAll('dialog');
            $.post("/comment/deleteBatch", {"ids": ids}, function (result) {
                if (result.success) {
                    layer.msg("删除成功！");
                    table.reload("commentListTable", {});
                } else {
                    layer.msg("删除失败，请联系管理员！");
                }
            }, "json");
        }, function () {

        });
    }
    /*]]>*/
</script>
</body>
</html>